package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.accident;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.QuotaConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.StrategyConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * description:
 * NPT015福运连年B意外险保额校验
 * 投保人年龄小于61岁且投被保人不是同一人且不是趸交，校验累积意外险保额
 *
 * @author jackdaw
 * @date 2020-03-30 18:03
 */
public class Npt015AccidentStrategy implements AccidentStrategy {
    @Override
    public InsuredModel checkAccidentInsuranceAmount(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository, ProductModel productModel, RedisService redisService, ProductConfig productConfig, Object object) {
        //保单信息
        PolicyInfoModel policyInfoModel = acceptRequestDataModel.getRequest().getPolicyInfo();
        //投保人
        AppntModel appntModel = acceptRequestDataModel.getRequest().getCustInfo().getAppnt();
        //被保人
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(productModel.getProdInsSeq(), acceptRequestDataModel.getRequest().getCustInfo().getInsurants().getInsurant());
        BigDecimal sum = BigDecimal.valueOf(0);
        if (!AcceptUtil.isSamePerson(insurantModel, appntModel) &&
                DateUtil.getYears(appntModel.getAppntBirthday(), policyInfoModel.getPolApplyDate()) <= UnderWriteConstant.AGE_SIXTY_YEAR &&
                !StringUtils.equals(acceptRequestDataModel.getRequest().getProductInfo().getPayIntv(), UnderWriteConstant.SINGLE_PAY)) {
            //计算当前单保额
            BigDecimal nowAmount = BigDecimal.valueOf(0);
            for (ProductModel product : acceptRequestDataModel.getRequest().getProductInfo().getProducts().getProduct()) {
                if (product.getProdInsSeq() == productModel.getProdInsSeq()) {
                    if (AcceptUtil.isStrategy(StrategyConstant.ACCIDENT_INSURANCE,
                            product.getProdCode(), productConfig.getAccumulatedRiskList())) {
                        nowAmount = nowAmount.add(AcceptUtil.getAccidentAmount(productModel, acceptRequestDataModel.getRequest()));
                    }
                }
            }
            //取缓存保额
            BigDecimal cacheAmount = redisService.getCacheQuota(QuotaConstant.SORT_F, appntModel.getIdNo(), nowAmount, acceptRequestDataModel.getRequest().getPolicyInfo().getContNo());
            BigDecimal historyAmount = BigDecimal.valueOf(0);
            if (StringUtils.isNotBlank(appntModel.getAppntNo())) {
                historyAmount = insuredCoreRepository.getHistoryAccidentInsuranceAmount(acceptRequestDataModel.getRequest().getPolicyInfo().getPrtNo(), appntModel.getAppntNo());
            }
            BigDecimal totalAmount = nowAmount.add(cacheAmount).add(historyAmount);
            sum = totalAmount;
            BigDecimal limit = BigDecimal.valueOf(3000000);
            if (totalAmount.compareTo(limit) == 1) {
                return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                        String.format(UnderWriteConstant.UNDER_WRITE_NYGG0009_MESSAGE, appntModel.getAppntName()));
            }
        }


        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE, sum);
    }
}
